<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>微信小程序</title>
	</head>
	<body style="font-size: 16px; margin: 0; padding: 0;">
		<div
			style="display: flex; justify-content: space-between; align-items: center; flex-direction: column; width: 100vw; height: 100vh; padding: 0 0 10vh 0; box-sizing: border-box;">
			<div style="display: flex; justify-content: center; align-items: center; margin-top: 20px;">
				<img style="width: 26px; height: 30px;"
					src="https://duanju-1257028257.cos.ap-guangzhou.myqcloud.com/images/1.png" alt="#">
				<span style="margin-left: 10px;">本链接经过SSL安全加密,请放心点击</span>
			</div>

			<div style="display: flex; justify-content: center; align-items: center; flex-wrap: wrap;">
				<img style="width: 100px; height: 80px;"
					src="https://duanju-1257028257.cos.ap-guangzhou.myqcloud.com/images/2.png" alt="#">
				<div style="width: 100%; text-align: center; font-size: 18px; margin-top: 18px;">正在跳转到微信...</div>
			</div>

			<div style="display: flex; justify-content: center; align-items: center; flex-wrap: wrap;">
				<div style="width: 100%; text-align: center; margin-bottom: 18px;">如未自动打开微信请点击下方按钮</div>
				<div style="width: 80%; text-align: center; background-color: rgb(0, 191, 94); color: white; line-height: 58px; font-size: 18px; border-radius: 10px;"
					onclick="openLink()">点击立即前往微信</div>
				<div style="width: 100%; height: 1px;"></div>
				<img style="width: 118px; height: 73px; margin: 18px 0;"
					src="https://duanju-1257028257.cos.ap-guangzhou.myqcloud.com/images/3.gif" alt="#">
				<div style="width: 100%; text-align: center;">点击上方 <span style="color: rgb(0, 191, 94);">绿色按钮</span>
					跳转到微信</div>
			</div>
		</div>
		<script>
			// 		需特别注意跨域问题和https问题，所有接口均需支持https,添加访问域名列表到云开发后台的跨域域名里
			var search = null
			var encodeurl = ''
			var appletLink = ''

			function openLink() {
			  if (appletLink) {
			    location.href = appletLink
			  }
			}

			function getCallbackInfo() {
			  let xhr = new XMLHttpRequest()
			  //每个项目的这个地址都不一样。都需要单独修改后再上传
			  // 测试环境
			  const domain = 'https://api.yunxianghuyu.com'
			  const api = '/h5/mini/link'
			  const requestUrl = domain + api
				
			  xhr.open('POST', requestUrl)
			  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
			  xhr.send('id=' + search.cid)
			  xhr.onreadystatechange = function() {
			    console.log('xhr.responseText=', xhr)
			    if (xhr.readyState == 4) {
			      let res = JSON.parse(xhr.responseText)
			      appletLink = res.data.url_link
			      location.href = appletLink
			    }
			  }
			}

			window.onload = function() {
			  let url = decodeURI(location.search)
			  if (url) {
			    encodeurl = encodeURIComponent(window.location.href)
			    search = {}
			    url = url.replace(/(\?|#)/g, '')
			    url = url.split('&')
			    url.forEach(item => {
			      let _item = item.split('=')
			      search[_item[0]] = _item[1]
			    })
			    if (search.cid) {
			      getCallbackInfo()
			    }
			  }
			}
		</script>
	</body>
</html>
